package com.ctrip.corp.obt.shard.infrastructure.apollo;


import com.ctrip.corp.obt.generic.apollo.TenantConfigService;
import com.ctrip.corp.obt.generic.core.context.TenantContext;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import lombok.Data;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

import static com.ctrip.corp.obt.generic.constants.GenericConstants.DEFAULT;


/**
 * @author marco.zheng
 * @version 1.0
 * @date 2022/03/15
 * @desc apollo client
 * @see
 * @since 1.0
 */
@Data
public class ApolloClient {

	public static final String DEFAULT_SYSTEM_MODEL = "single";

	public static final String MULTI_TENANT_SYSTEM_MODEL = "multiTenant";

	public static final String COMMON_REQUEST_HEADER = "tenant_id";

	public static final String SINGLE_SYSTEM_ERROR = "当前系统为非租户模式，发现租户id:[%s]，系统模式错误，请确认系统模式";

	public static final String MULTI_SYSTEM_ERROR = "当前系统为租户模式，未发现租户id，系统模式错误，请确认系统模式";

	public static final String NAMESPACE_APPLICATION = "application";

	public static final String NAMESPACE_COMMON = "corpgovernment.common";

	/**
	 * 获取租户列表
	 *
	 * @return
	 */
	public List<String> getTenantList() {
		return Arrays.asList(ConfigService.getConfig(NAMESPACE_COMMON).getProperty("apollo.multitenantmeta.tenant", "").split(","));
	}

	/**
	 * druidClassName
	 *
	 * @return
	 */
	public String getDriverClassName() {
		return getCommonProperty("spring.druid.druidClassName", null);
	}

	/**
	 * url
	 *
	 * @return
	 */
	public String getDatabaseUrl() {
		return getCommonProperty("spring.druid.url", null);
	}

	/**
	 * password
	 *
	 * @return
	 */
	public String getDatabasePassword() {
		return getCommonProperty("spring.druid.password", null);
	}

	/**
	 * username
	 *
	 * @return
	 */
	public String getDatabaseUsername() {
		return getCommonProperty("spring.druid.username", null);
	}


	/**
	 * initialSize
	 *
	 * @return
	 */
	public Integer getDatabaseInitialSize() {
		return Integer.valueOf(getCommonProperty("spring.druid.initialSize", "10"));
	}

	/**
	 * maxActive
	 *
	 * @return
	 */
	public Integer getDatabaseMaxActive() {
		return Integer.valueOf(getCommonProperty("spring.druid.maxActive", "30"));
	}

	/**
	 * minIdle
	 *
	 * @return
	 */
	public Integer getDatabaseMinIdle() {
		return Integer.valueOf(getCommonProperty("spring.druid.minIdle", "20"));
	}

	/**
	 * maxWait
	 *
	 * @return
	 */
	public Integer getDatabaseMaxWait() {
		return Integer.valueOf(getCommonProperty("spring.druid.maxWait", "30000"));
	}


	public String getSystemModel() {
		return ConfigService.getConfig(NAMESPACE_COMMON).getProperty("system_model", DEFAULT_SYSTEM_MODEL);
	}

	/**
	 * 获取common 配置
	 *
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	public String getCommonProperty(String key, String defaultVal) {
		return this.getProperty(key, defaultVal, this::getCommonConfig);
	}


	/**
	 * 获取阿波罗配置
	 *
	 * @param key
	 * @param defaultVal
	 * @return
	 */
	private String getProperty(String key, String defaultVal, Supplier<Config> supplier) {
//		String systemModel = getSystemModel();
//		String tenantId = TenantContext.getTenantId();
//		if (Objects.equals(DEFAULT_SYSTEM_MODEL, systemModel)) {
//			// 当前为单租户模式
//			if (StringUtils.isNotBlank(tenantId) && !Objects.equals(tenantId, DEFAULT)) {
//				throw new ShardException(String.format(SINGLE_SYSTEM_ERROR, tenantId));
//			}
//		} else {
//			if (StringUtils.isBlank(tenantId)) {
//				throw new ShardException(MULTI_SYSTEM_ERROR);
//			}
//		}
		return supplier.get().getProperty(key, defaultVal);
	}

	/**
	 * 获取common config
	 *
	 * @return
	 */
	private Config getCommonConfig() {
		String systemModel = getSystemModel();
		String tenantId = TenantContext.getTenantId();
		if (Objects.equals(DEFAULT_SYSTEM_MODEL, systemModel) || Objects.equals(tenantId, DEFAULT)) {
			// 当前为单租户模式
			return ConfigService.getConfig(NAMESPACE_COMMON);
		} else {
			return TenantConfigService.getConfig(tenantId, NAMESPACE_COMMON);
		}
	}

}
